package com.hntx.joymusic.util;

import android.content.SharedPreferences;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;

import com.hntx.joymusic.MyApp;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public final class SPUtils {
    private static Map<String, SPUtils> sSPMap = new HashMap();
    private SharedPreferences sp;

    private SPUtils(String name) {
        this.sp = MyApp.getInstance().getSharedPreferences(name, 0);
    }

    public static SPUtils getInstance() {
        return getInstance("");
    }

    public static SPUtils getInstance(String name) {
        if (isSpace(name))
            name = "spUtils";
        SPUtils spUtils = (SPUtils) sSPMap.get(name);
        if (spUtils == null) {
            spUtils = new SPUtils(name);
            sSPMap.put(name, spUtils);
        }
        return spUtils;
    }

    private static boolean isSpace(String str) {
        if (str == null)
            return true;
        int i = str.length();
        for (int j = 0; j < i; j++)
            if (!Character.isWhitespace(str.charAt(j)))
                return false;
        return true;
    }

    public void clear() {
        sp.edit().clear().apply();
    }

    public boolean contains(@NonNull String paramString) {
        return sp.contains(paramString);
    }

    public SharedPreferences.Editor edit() {
        return sp.edit();
    }

    public Map<String, ?> getAll() {
        return sp.getAll();
    }

    public boolean getBoolean(@NonNull String name) {
        return getBoolean(name, false);
    }

    public boolean getBoolean(@NonNull String str, boolean flag) {
        return sp.getBoolean(str, flag);
    }

    public float getFloat(@NonNull String f) {
        return getFloat(f, -1.0F);
    }

    public float getFloat(@NonNull String paramString, float paramFloat) {
        return sp.getFloat(paramString, paramFloat);
    }

    public int getInt(@NonNull String paramString) {
        return getInt(paramString, -1);
    }

    public int getInt(@NonNull String paramString, int paramInt) {
        return this.sp.getInt(paramString, paramInt);
    }

    public long getLong(@NonNull String paramString) {
        return getLong(paramString, -1L);
    }

    public long getLong(@NonNull String paramString, long paramLong) {
        return this.sp.getLong(paramString, paramLong);
    }

    public String getString(@NonNull String paramString) {
        return getString(paramString, "");
    }

    public String getString(@NonNull String paramString1, @NonNull String paramString2) {
        return this.sp.getString(paramString1, paramString2);
    }



    public void put(@NonNull String paramString, float paramFloat) {
        this.sp.edit().putFloat(paramString, paramFloat).apply();
    }

    public void put(@NonNull String paramString, int paramInt) {
        this.sp.edit().putInt(paramString, paramInt).apply();
    }

    public void put(@NonNull String paramString, long paramLong) {
        this.sp.edit().putLong(paramString, paramLong).apply();
    }

    public void put(@NonNull String paramString1, @NonNull String paramString2) {
        this.sp.edit().putString(paramString1, paramString2).apply();
    }

    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    public void put(@NonNull String paramString, @NonNull Set<String> paramSet) {
        this.sp.edit().putStringSet(paramString, paramSet).apply();
    }

    public void put(@NonNull String paramString, boolean paramBoolean) {
        this.sp.edit().putBoolean(paramString, paramBoolean).apply();
    }

    public void remove(@NonNull String paramString) {
        this.sp.edit().remove(paramString).apply();
    }
}